﻿using System;
using System.Globalization;

//For : Obtaining brushes through reflection
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;                   //For : InkCanvas
using System.Windows.Media;
using BitsOfStuff.Support;

namespace BitsOfStuff
{
    /// <summary>
    /// Class for setings window
    /// </summary>
    public partial class StylusSettings
    {
        Color _currColor = Colors.Black;
        double _penWidth = 6;
        double _penHeight = 6;
        private bool _initialized = false;

        private double _displayTempParam = 0;

        /// <summary>
        /// Constructor of stylus setting
        /// </summary>
        public StylusSettings()
        {
            this.InitializeComponent(); inicialize();
            // Insert code required on object creation below this point.
        }

        private void b_Click(object sender, RoutedEventArgs e)
        {
            var sb = (SolidColorBrush)(sender as Button).Background;
            _currColor = sb.Color;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        // Public property initializes controls and returns their values.
        /// <summary>
        /// property of all drawing Attributes
        /// </summary>
        public DrawingAttributes drawingAttributes
        {
            set
            {
                value.IgnorePressure = false;
                //chkHighlight.IsChecked = value.IsHighlighter;
                _penWidth = 6;
                _penHeight = 6;
                _currColor = value.Color;
            }
            get
            {
                DrawingAttributes drawattr = new DrawingAttributes();
                drawattr.IgnorePressure = false;
                drawattr.Width = 6;
                drawattr.Height = 6;
                //drawattr.IsHighlighter = (bool)chkHighlight.IsChecked;
                drawattr.Color = _currColor;
                return drawattr;
            }
        }

        private void cornerAnalysisPrecisionSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_initialized)
                MyParameters.cornerHighCurvedPrecisionParameter = cornerAnalysisPrecisionSlider.Value;
            _displayTempParam = Math.Round(cornerAnalysisPrecisionSlider.Value, 2);
            cornerAnalysisPrecionLabel.Text = _displayTempParam.ToString(CultureInfo.InvariantCulture);
        }

        private void minimalRecognizeAngleSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_initialized)
                MyParameters.cornerMinimalRecognizedAngle = minimalRecognizeAngleSlider.Value;
            _displayTempParam = Math.Round(minimalRecognizeAngleSlider.Value, 2);
            minimalRecognizeAngleLabel.Text = _displayTempParam.ToString(CultureInfo.InvariantCulture);
        }

        private void curvedHandToleranceSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_initialized)
                MyParameters.supportHandParabolTolerance = curvedHandToleranceSlider.Value;
            _displayTempParam = Math.Round(curvedHandToleranceSlider.Value, 2);
            curvedHandToleranceLabel.Text = _displayTempParam.ToString(CultureInfo.InvariantCulture);
        }

        private void curvedMargeDistanceSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_initialized)
                MyParameters.curvedMargetDistance = (int)curvedMargeDistanceSlider.Value;
            int smallParam = (int)curvedMargeDistanceSlider.Value;
            curvedMargeDistanceLabel.Text = smallParam.ToString(CultureInfo.InvariantCulture);
        }

        private void inicialize()
        {
            cornerAnalysisPrecisionSlider.Value = MyParameters.cornerHighCurvedPrecisionParameter;
            minimalRecognizeAngleSlider.Value = MyParameters.cornerMinimalRecognizedAngle;
            curvedHandToleranceSlider.Value = MyParameters.supportHandParabolTolerance;
            curvedMargeDistanceSlider.Value = MyParameters.curvedMargetDistance;
            dpSlider.Value = MyParameters.pdtolerance;

            axisAngleSlider.Value = MyParameters.shapeStartAngle - MyParameters._shapeStartAngle;
            userAngleSlider.Value = MyParameters.shapeAxesDiversion;

            moveAxes(axisAngleSlider.Value);
            moveToleranceLine(userAngleSlider.Value);
            _initialized = true;
            var apps = SupportFunctions.getTaskWindows();
            foreach (var app in apps)
            {
                appSelector.Items.Add(app);
            }
        }

        //Function to set slider and label to default
        private void cornerAnalysisPrecisionSliderToDefault()
        {
            //changeSliderValueFluentThread(cornerAnalysisPrecisionSlider, MyParameters._highCurvedPrecisionParameter);
            cornerAnalysisPrecisionSlider.Value = MyParameters._highCurvedPrecisionParameter;
            MyParameters.cornerHighCurvedPrecisionParameter = MyParameters._highCurvedPrecisionParameter;
        }

        private void minimalRecognizeAngleSliderToDefault()
        {
            minimalRecognizeAngleSlider.Value = MyParameters._minimalRecognizedAngle;
            MyParameters.cornerMinimalRecognizedAngle = MyParameters._minimalRecognizedAngle;
        }

        private void curvedHandToleranceSliderToDefault()
        {
            curvedHandToleranceSlider.Value = MyParameters._handParabolTolerance;
            MyParameters.supportHandParabolTolerance = MyParameters._handParabolTolerance;
        }

        private void curvedMargeDistanceSliderToDefault()
        {
            curvedMargeDistanceSlider.Value = MyParameters._curvedMargetDistance;
            MyParameters.curvedMargetDistance = MyParameters._curvedMargetDistance;
        }

        private void dpSliderToDefault()
        {
            dpSlider.Value = MyParameters._pdtolerance;
            MyParameters.pdtolerance = MyParameters._pdtolerance;
        }

        private void userAngleToDefault()
        {
            userAngleSlider.Value = MyParameters._shapeAxesDiversion;
            MyParameters.shapeAxesDiversion = MyParameters._shapeAxesDiversion;
        }

        private void axisAngleToDefault()
        {
            axisAngleSlider.Value = MyParameters._shapeStartAngle - MyParameters._shapeStartAngle;
            MyParameters.shapeStartAngle = MyParameters._shapeStartAngle;
        }

        //Double click to set slider and value to default
        //Corner parameters
        private void cornerAnalysisPrecionNameMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_initialized)
                cornerAnalysisPrecisionSliderToDefault();
        }

        private void minimalRecognizeAngleName_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_initialized)
                minimalRecognizeAngleSliderToDefault();
        }

        private void curvedMargeDistanceSliderDragLeave(object sender, DragEventArgs e)
        {
        }

        //Curved parameters

        private void curvedHandToleranceName_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_initialized)
                curvedHandToleranceSliderToDefault();
        }

        private void curvedAnalysisLabel_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            curvedHandToleranceSliderToDefault();
            curvedMargeDistanceSliderToDefault();
            dpSliderToDefault();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void curvedMargeDistanceName_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            curvedMargeDistanceSliderToDefault();
        }

        private void dpSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_initialized)
                MyParameters.pdtolerance = (int)dpSlider.Value;
            dpLabel.Text = MyParameters.pdtolerance.ToString(CultureInfo.InvariantCulture);
        }

        private void cornerAnalysisLabel_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            cornerAnalysisPrecisionSliderToDefault();
        }

        private void axisAngleSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_initialized)
            {
                MyParameters.shapeStartAngle = MyParameters._shapeStartAngle + (int)axisAngleSlider.Value;
                moveAxes(axisAngleSlider.Value);
            }
            axisAngleLabel.Text = ((int)axisAngleSlider.Value).ToString(CultureInfo.InvariantCulture);
        }

        private void userAngleSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_initialized)
            {
                MyParameters.shapeAxesDiversion = (int)userAngleSlider.Value;
                moveToleranceLine(userAngleSlider.Value);
            }
            userAngleLabel.Text = ((int)userAngleSlider.Value).ToString(CultureInfo.InvariantCulture);
        }

        private void dpName_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            dpSliderToDefault();
        }

        private void userAngleName_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            userAngleToDefault();
        }

        private void axisAngleName_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            axisAngleToDefault();
        }

        private void angleLabel_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            userAngleToDefault();
            axisAngleToDefault();
        }

        private void cornerAnalysisLabel_MouseDoubleClick_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            cornerAnalysisPrecisionSliderToDefault();
            minimalRecognizeAngleSliderToDefault();
        }

        private void moveToleranceLine(double angle)
        {
            RotateTransform rotateTransform = new RotateTransform(angle + axisAngleSlider.Value);
            rightToleranceLine.RenderTransform = rotateTransform;
            rotateTransform = new RotateTransform(-angle + axisAngleSlider.Value);
            leftToleranceLine.RenderTransform = rotateTransform;
        }

        private void moveAxes(double angle)
        {
            RotateTransform rotateTransform = new RotateTransform(angle);
            axeX.RenderTransform = rotateTransform;
            axeY.RenderTransform = rotateTransform;
            moveToleranceLine(userAngleSlider.Value);
        }

        private void changeSliderValueFluent(Slider slider, double newValue)
        {
        }

        private void changeSliderValueFluentThread(Slider slider, double newValue)
        {
            double distance = slider.Value - newValue;
            double oneStep = distance / 10;
            while (Math.Abs(slider.Value - newValue) > 0.0001)
            {
                slider.Value = slider.Value - oneStep;
                Thread.Sleep(100);
            }
        }

        private void appSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MyParameters.selectedWindow = appSelector.SelectedItem.ToString();
        }
    }
}